# -*- bazel -*-

load("@drake//tools/install:install.bzl", "install")
load("@drake//tools/skylark:cc.bzl", "cc_library")
load(
    "@drake//tools/workspace:vendor_cxx.bzl",
    "cc_library_vendored",
    "generate_vendor_patch",
)

licenses(["notice"])  # Qhull

package(default_visibility = ["//visibility:private"])

_HDRS_C = glob([
    "src/libqhull_r/*.h",
])

_HDRS_CPP = glob([
    "src/libqhullcpp/*.h",
])

_SRCS_C = [
    "src/libqhull_r/geom2_r.c",
    "src/libqhull_r/geom_r.c",
    "src/libqhull_r/global_r.c",
    "src/libqhull_r/io_r.c",
    "src/libqhull_r/libqhull_r.c",
    "src/libqhull_r/mem_r.c",
    "src/libqhull_r/merge_r.c",
    "src/libqhull_r/poly2_r.c",
    "src/libqhull_r/poly_r.c",
    "src/libqhull_r/qset_r.c",
    "src/libqhull_r/random_r.c",
    "src/libqhull_r/stat_r.c",
    "src/libqhull_r/user_r.c",
    "src/libqhull_r/usermem_r.c",
]

_SRCS_CPP = [
    "src/libqhullcpp/Qhull.cpp",
    "src/libqhullcpp/QhullFacet.cpp",
    "src/libqhullcpp/QhullFacetList.cpp",
    "src/libqhullcpp/QhullFacetSet.cpp",
    "src/libqhullcpp/QhullHyperplane.cpp",
    "src/libqhullcpp/QhullPoint.cpp",
    "src/libqhullcpp/QhullPointSet.cpp",
    "src/libqhullcpp/QhullQh.cpp",
    "src/libqhullcpp/QhullRidge.cpp",
    "src/libqhullcpp/QhullSet.cpp",
    "src/libqhullcpp/QhullUser.cpp",
    "src/libqhullcpp/QhullVertex.cpp",
    "src/libqhullcpp/QhullVertexSet.cpp",
    "src/libqhullcpp/RoadError.cpp",
    "src/libqhullcpp/RoadLogEvent.cpp",
]

cc_library(
    name = "qhull_r",
    hdrs = _HDRS_C,
    copts = [
        "-fvisibility=hidden",
        "-w",
    ],
    includes = ["src"],
    isystem = True,
    srcs = _SRCS_C,
    linkstatic = 1,
)

_HDRS_CPP_VENDORED = [
    x.replace("src/libqhullcpp/", "drake_hdr/libqhullcpp/")
    for x in _HDRS_CPP
]

_SRCS_CPP_VENDORED = [
    x.replace("src/", "drake_src/")
    for x in _SRCS_CPP
]

cc_library_vendored(
    name = "qhull",
    hdrs = _HDRS_CPP,
    hdrs_vendored = _HDRS_CPP_VENDORED,
    includes = ["drake_hdr"],
    isystem = True,
    srcs = _SRCS_CPP,
    srcs_vendored = _SRCS_CPP_VENDORED,
    copts = ["-w"],
    linkstatic = 1,
    visibility = ["//visibility:public"],
    deps = [":qhull_r"],
)

generate_vendor_patch(
    name = "drake_qhull.patch",
    srcs = _HDRS_CPP + _SRCS_CPP,
    srcs_vendored = _HDRS_CPP_VENDORED + _SRCS_CPP_VENDORED,
    extra_prologue = """
The vendor_cxx tool was created by Jeremy Nimmer and the patch file is
contemporaneous with whichever release of Drake it accompanies.""",
)

# Install the license file.
install(
    name = "install",
    docs = [
        "COPYING.txt",
        # We must redistribute our code changes, per the Qhull license:
        # ...
        # 3. If you modify Qhull, you must include a notice giving the
        #    name of the person performing the modification, the date of
        #    modification, and the reason for such modification.
        # ...
        ":drake_qhull.patch",
        "@drake//tools/workspace/qhull_internal:patches",
    ],
    allowed_externals = [
        "@drake//tools/workspace/qhull_internal:patches",
    ],
    doc_strip_prefix = ["patches"],
    visibility = ["//visibility:public"],
)

exports_files(["drake_repository_metadata.json"])
